﻿using System;
using System.Windows;
using System.Windows.Media.Animation;

namespace Gmantis.Controls.Chart
{
    public class PlotElementAnimation : DependencyObject
    {
        // Fields
        public static readonly DependencyProperty IndexDelayProperty = DependencyProperty.RegisterAttached("IndexDelay", typeof(double), typeof(PlotElementAnimation), new PropertyMetadata(0.0));
        public static readonly DependencyProperty StoryboardProperty = DependencyProperty.Register("Storyboard", typeof(Storyboard), typeof(PlotElementAnimation), new PropertyMetadata(null));
        public static readonly DependencyProperty SymbolStyleProperty = DependencyProperty.Register("SymbolStyle", typeof(Style), typeof(PlotElementAnimation), new PropertyMetadata(null));

        // Methods
        public static double GetIndexDelay(DependencyObject obj)
        {
            return (double)obj.GetValue(IndexDelayProperty);
        }

        public static void SetIndexDelay(DependencyObject obj, double value)
        {
            obj.SetValue(IndexDelayProperty, value);
        }

        internal void Start(PlotElement pe)
        {
            int length = ((IDataSeriesInfo)pe.DataPoint.Series).GetValues().GetLength(1);
            if (this.SymbolStyle != null)
            {
                pe.Style = this.SymbolStyle;
            }
            if (this.Storyboard != null)
            {
                Storyboard storyboard = this.Storyboard.DeepClone<Storyboard>();
                if (storyboard != null)
                {
                    foreach (Timeline timeline in this.Storyboard.Children)
                    {
                        Timeline element = timeline.DeepClone<Timeline>();
                        PropertyPath targetProperty = Storyboard.GetTargetProperty(timeline);
                        Storyboard.SetTargetProperty(element, targetProperty);
                        double indexDelay = GetIndexDelay(timeline);
                        if (indexDelay != 0.0)
                        {
                            double num3 = ((pe.DataPoint.PointIndex * indexDelay) * element.Duration.TimeSpan.TotalSeconds) / ((double)length);
                            element.BeginTime = new TimeSpan?(TimeSpan.FromSeconds(num3));
                        }
                        storyboard.Children.Add(element);
                    }
                    Storyboard.SetTarget(storyboard, pe);
                    storyboard.Begin();
                }
            }
        }

        // Properties
        public Storyboard Storyboard
        {
            get
            {
                return (Storyboard)base.GetValue(StoryboardProperty);
            }
            set
            {
                base.SetValue(StoryboardProperty, value);
            }
        }

        public Style SymbolStyle
        {
            get
            {
                return (Style)base.GetValue(SymbolStyleProperty);
            }
            set
            {
                base.SetValue(SymbolStyleProperty, value);
            }
        }
    }
}
